package net.zjitc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import net.zjitc.entity.Manger;
import net.zjitc.entity.Vo.MangerRoleVo;
import net.zjitc.mapper.ManagerMapper;
import net.zjitc.service.MangerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Transactional
@Service
public class IMangerService implements MangerService {
    @Autowired
    private ManagerMapper mangerMapper;
    @Override
    public List<Manger> selectAll() {
        return mangerMapper.selectList(null);
    }

    @Override
    public Page<Manger> selectPage(Integer pagenum, Integer pagesize) {
        Page<Manger> page=new Page<>(pagenum,pagesize);
        page=mangerMapper.selectPage(page,null);
        return page;
    }
    @Override
    public Page<MangerRoleVo> selectPageVo(Page page) {
        Page<MangerRoleVo> vos = mangerMapper.selectManageVo(page);
        return vos;
    }

    @Override
    public Manger selectByUsername(String username) {
        QueryWrapper<Manger> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("mg_name",username);
        Manger manger = mangerMapper.selectOne(queryWrapper);
        return manger;
    }

    @Override
    public Integer addUser(MangerRoleVo mangerRoleVo) {
        long l = System.currentTimeMillis();
        Date date=new Date(l);
        mangerRoleVo.setType(0);
        mangerRoleVo.setMg_time((int)(l/1000));
        int insert = mangerMapper.insert(mangerRoleVo);
        mangerRoleVo.setMg_time(null);
        mangerRoleVo.setCreate_time(date);
        mangerRoleVo.setPassword(null);
        return insert;
    }

    @Override
    public Manger selectById(Integer id) {
        QueryWrapper<Manger> wrapper=new QueryWrapper<>();
        //只查某些字段
        wrapper.select("mg_id AS id,mg_name AS username,role_id,mg_mobile AS mobile,mg_email AS email");
        //不查某些字段
        //wrapper.select(Manger.class,info->!info.getProperty().equals("password"));
        wrapper.eq("mg_id",id);
        Manger manger = mangerMapper.selectOne(wrapper);
        return manger;
    }

    @Override
    public Integer updateType(Integer id, boolean type) {
        Integer myInt = type ? 1 : 0;
        MangerRoleVo mangerRoleVo=new MangerRoleVo();
        mangerRoleVo.setType(myInt);
        QueryWrapper<Manger> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("mg_id",id);
        int update = mangerMapper.update(mangerRoleVo, queryWrapper);
        return update;
    }

    @Override
    public Integer updateUser(Integer id, Manger manger) {
        QueryWrapper<Manger> wrapper=new QueryWrapper<>();
        wrapper.eq("mg_id",id);
        int i = mangerMapper.update(manger,wrapper);
        return i;
    }

    @Override
    public Integer deleteUser(Integer id) {
        int i = mangerMapper.deleteById(id);
        return i;
    }

    @Override
    public Integer assignPer(Integer id, Integer rid) {
        MangerRoleVo mangerRoleVo=new MangerRoleVo();
        mangerRoleVo.setRole_id(rid);
        QueryWrapper<Manger> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("mg_id",id);
        int update = mangerMapper.update(mangerRoleVo, queryWrapper);
        return update;
    }

    @Override
    public Manger login(String usernamem, String password) {
        QueryWrapper<Manger> wrapper=new QueryWrapper<>();
        wrapper.eq("mg_name",usernamem).eq("mg_pwd",password);
        Manger manger = mangerMapper.selectOne(wrapper);
        return manger;
    }

}
